package com.ruoyi.thread.controller;

import com.ruoyi.thread.service.ThreadO1Service;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.CompletableFuture;

@RestController
@Slf4j
public class ThreadO1Controller {
    @Autowired
    ThreadO1Service threadO1Service;

    @PostMapping("/asyncService")
    public String asyncService(){
        long startTime = System.currentTimeMillis();
        log.info("当前任务开始时间为：");
        CompletableFuture<String> service1 = threadO1Service.serviceOne();
        CompletableFuture<String> service2 = threadO1Service.serviceTwo();
        CompletableFuture.allOf(service1,service2).join();
        try {
            service1.get();
            service2.get();
        } catch (Exception e){
            e.printStackTrace();
        }
        long endTime = System.currentTimeMillis();
        log.info("当前任务耗时为ms：{}",endTime-startTime);

        return "当前任务耗时为ms："+(endTime-startTime);

    }
}
